package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.IBitmapDrawable;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.BlurFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	
	import mx.graphics.codec.PNGEncoder;
	
	import spark.effects.Scale;

	public class AutoResizeForPng
	{
		private static var w:int;
		private static var h:int;
		
		// 自动适应裁剪
		public static function autoResize(data:Array) : Array
		{
			var resizeData:Array = [];
			var rectangle:Rectangle = getMaxRect(data);
			
			var bitmap:Bitmap;
			var targetBitmapData:BitmapData;
			var bp:Bitmap;
			for (var url:String in data)
			{
				bitmap = data[url];
				
				targetBitmapData = new BitmapData(rectangle.width,rectangle.height, true, 0x00000000);
				
				targetBitmapData.copyPixels(bitmap.bitmapData, rectangle, new Point(0, 0));
				//draw(targetBitmapData, bitmap.bitmapData, rectangle, new Point(0, 0));
				
				bp = new Bitmap(targetBitmapData, "auto", true)
				resizeData[url] = bp;
			}			
			
			return resizeData;
		}
		
		/**
		 * 获取最大裁剪区域 
		 * @param data
		 * @return 
		 * 
		 */		
		public static function getMaxRect(data:Array) : Rectangle
		{
			var rectangle : Rectangle;
			var rect : Rectangle;
			for each(var obj:Bitmap in  data)
			{
				if(!rectangle)
				{
					rectangle = getBoundsRect(obj.bitmapData);
					continue;
				}
				
				rect = getBoundsRect(obj.bitmapData);
				
				if (rectangle.left > rect.left)
				{
					rectangle.x = rect.x;
				}
				
				if (rectangle.top > rect.top)
				{
					rectangle.y = rect.y;
				}
				
				if (rectangle.right < rect.right)
				{
					rectangle.width = rect.right - rectangle.x;
				}
				
				if (rectangle.bottom < rect.bottom)
				{
					rectangle.height = rect.bottom - rectangle.y;
				}
			}
			
			rectangle = getRectEven(rectangle);
			
			return rectangle;
		}
		
		public static function getRectEven(rect:Rectangle) : Rectangle
		{
			if (!checkEven(rect.x))
			{
				rect.x -= 1;
				rect.width += 1;
			}
			
			if (!checkEven(rect.y))
			{
				rect.y -= 1;
				rect.height += 1;
			}
			
			if (!checkEven(rect.width))
			{
				rect.width += 1;
			}
			
			if (!checkEven(rect.height))
			{
				rect.height += 1;
			}
			
			return rect;
		}
		
		
		
		/**
		 * 获取偶数大小（向上取整） 
		 * @param value
		 * @return 
		 * 
		 */		
		private static function checkEven(value:int) : Boolean
		{
			if (value%2 == 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		/**
		 * 获取像素区域
		 * @param bmd
		 * @return 
		 * 
		 */		
		public static function getBoundsRect(bmd:BitmapData) : Rectangle
		{
			return bmd.getColorBoundsRect(0xFF000000, 0x00000000, false);
		}
		
		/**
		 * draw版的copyPixels 
		 * @param target
		 * @param source
		 * @param rect
		 * @param pt
		 * @param colorTransform
		 * @param scale
		 * 
		 */		
		public static function draw(target:BitmapData, source:IBitmapDrawable, rect:Rectangle, pt:Point, colorTransform:ColorTransform = null, scale:Number = 1):void
		{
			var clipRect:Rectangle = new Rectangle();
			var matrix:Matrix = new Matrix();
			clipRect.x = pt.x;
			clipRect.y = pt.y;
			clipRect.width = rect.width * scale;
			clipRect.height = rect.height * scale;
			matrix.tx = pt.x - rect.x;
			matrix.ty = pt.y - rect.y;
			matrix.a = matrix.d = scale; //x,y分别缩放
			target.draw(source, matrix, colorTransform, null, clipRect, true);
		}
	}
}